package com.rabbit.topics;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by IntelliJ IDEA.
 * <p>
 * Author: libingyang
 * Date: 2021-03-15 15:35
 */
public class Producer {


    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("192.168.100.54");

        factory.setPort(5672);

        factory.setVirtualHost("/");

        factory.setUsername("guest");

        factory.setPassword("guest");

        // 获取连接对象
        Connection connection = factory.newConnection();

        // 获取连接通道
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("topics", "topic");




        String message = "世界你好！";
        String routingKey = "user.save";

        // 发送消息
        /**
         * 1 交换机名称
         * 2 队列名称
         * 3 传递消息的额外设置
         * 4 具体的消息内容
         */
        for(int i=0;i<=20;i++) {
            channel.basicPublish("topics", routingKey, null, (i+message).getBytes());
        }

        channel.close();
        connection.close();

    }
}
